package com.woniuxy.controllers;

import com.woniuxy.entitys.Client;
import com.woniuxy.entitys.RestPOJO;
import com.woniuxy.services.ClientService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Api(tags = "客户联系人管理接口")
public class ClienController {

    @Autowired
    private ClientService clientService;


//    @GetMapping("/clients")
//    public RestPOJO getAll(){
//        List<Client> clientList = clientService.selAll();
//        RestPOJO restPOJO = new RestPOJO();
//        if(clientList != null){
//            restPOJO.setMessage("成功");
//            restPOJO.setStatus(200);
//            restPOJO.setData(clientList);
//        }else {
//            restPOJO.setMessage("失败");
//            restPOJO.setStatus(250);
//        }
//        return restPOJO;
//    }

    @ApiOperation(
            value = "根据公司id查询联系人信息集合",
            httpMethod = "GET",
            protocols = "HTTP",
            produces = "application/json",
            response = RestPOJO.class
    )
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="path",name="com_id",value="公司id",required=true)
    })
    //根据公司id查询联系人信息集合
    @GetMapping("/clients/{companyId}")
    public RestPOJO getAll(@PathVariable("companyId") String com_id){
        List<Client> clientList = clientService.selAllByComId(com_id);
        RestPOJO restPOJO = new RestPOJO();
        if(clientList != null){
            restPOJO.setMessage("成功");
            restPOJO.setStatus(200);
            restPOJO.setData(clientList);
        }else {
            restPOJO.setMessage("失败");
            restPOJO.setStatus(250);
        }
        return restPOJO;
    }

    @ApiOperation(
            value = "查询联系人信息",
            httpMethod = "GET",
            protocols = "HTTP",
            produces = "application/json",
            response = RestPOJO.class
    )
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="path",name="cli_id",value="联系人id",required=true)
    })
    //查询联系人信息
    @GetMapping("/clients/onlyOne/{cli_id}")
    public RestPOJO selectByPrimaryKey(@PathVariable("cli_id") Integer cli_id){
        Client client = clientService.selectByPrimaryKey(cli_id);
        RestPOJO restPOJO = new RestPOJO();
        if(client != null){
            restPOJO.setMessage("成功");
            restPOJO.setStatus(200);
            restPOJO.setData(client);
        }else {
            restPOJO.setMessage("失败");
            restPOJO.setStatus(250);
        }
        return restPOJO;
    }

    @ApiOperation(
            value = "保存修改的联系人信息",
            httpMethod = "PUT",
            protocols = "HTTP",
            produces = "application/json",
            response = RestPOJO.class
    )
    //保存修改
    @PutMapping("/clients")
    public RestPOJO updateByPrimaryKey(@RequestBody Client client){
//        System.out.println(client.getCli_sex()+"1111111111111111111111111111111111111111111111111111111111111111");
//        if(client.getCli_sex().equals("男")){
//            client.setCli_sex(2);
//        }
//        if(client.getCli_sex().equals("女")){
//            client.setCli_sex(1);
//        }
        Integer res = clientService.updateByPrimaryKey(client);
        RestPOJO restPOJO = new RestPOJO();
        if(res != null){
            restPOJO.setMessage("成功");
            restPOJO.setStatus(200);
        }else {
            restPOJO.setMessage("失败");
            restPOJO.setStatus(250);
        }
        return restPOJO;
    }

    @ApiOperation(
            value = "删除联系人（status改为0）",
            httpMethod = "POST",
            protocols = "HTTP",
            produces = "application/json",
            response = RestPOJO.class
    )
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="path",name="cli_id",value="联系人id",required=true)
    })
    //删除联系人（status = 0）
    @PostMapping("/clients/del/{cli_id}")
    public RestPOJO del(@PathVariable("cli_id") String cli_id){
        String cli_status = "0";
        Integer res = clientService.updCliStatus(cli_id,cli_status);
        RestPOJO restPOJO = new RestPOJO();
        if(res != null){
            restPOJO.setMessage("成功");
            restPOJO.setStatus(200);
        }else {
            restPOJO.setMessage("失败");
            restPOJO.setStatus(250);
        }
        return restPOJO;
    }

    @ApiOperation(
            value = "根据名字（cli_name）和公司id查询联系人信息",
            httpMethod = "GET",
            protocols = "HTTP",
            produces = "application/json",
            response = RestPOJO.class
    )
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query",name="name",value="联系人名字",required=true),
            @ApiImplicitParam(paramType="query",name="com_id",value="公司id",required=true)
    })
    //查询（根据名字）
    @GetMapping("/clientsSearch")
    public RestPOJO search( String name,  String com_id){
        name = "%" + name + "%";
        List<Client> clientList = clientService.searchById(name,com_id);
        RestPOJO restPOJO = new RestPOJO();
        if(clientList != null){
            restPOJO.setMessage("成功");
            restPOJO.setStatus(200);
            restPOJO.setData(clientList);
        }else {
            restPOJO.setMessage("失败");
            restPOJO.setStatus(250);
        }
        return restPOJO;
    }

    @ApiOperation(
            value = "根据名字（cli_name）和公司id查询联系人信息",
            httpMethod = "POST",
            protocols = "HTTP",
            produces = "application/json",
            response = RestPOJO.class
    )
    //新增
    @PostMapping("/clientsAdd")
    public RestPOJO add(@RequestBody Client client){
        Integer result = clientService.addClient(client);
        RestPOJO restPOJO = new RestPOJO();
        //开发计划成功后，新增联系人成功后，将id回传给前台服务器
        if(result > 0){
            restPOJO.setData(client.getCli_id());
            restPOJO.setMessage("成功");
            restPOJO.setStatus(200);
        }else {
            restPOJO.setMessage("失败");
            restPOJO.setStatus(250);
        }
        return restPOJO;
    }
}
